package qldc.pm.qlktcsdldc.service.impl;

import java.util.Date;
import java.util.List;

import qldc.pm.qlktcsdldc.model.DanhMucSoBanNganh;
import qldc.pm.qlktcsdldc.model.DanhMucSoBanNganh_TruongCongDan;
import qldc.pm.qlktcsdldc.service.base.DanhMucSoBanNganh_TruongCongDanLocalServiceBaseImpl;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.User;

/**
 * The implementation of the danh muc so ban nganh_ truong cong dan local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link qldc.pm.qlktcsdldc.service.DanhMucSoBanNganh_TruongCongDanLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see qldc.pm.qlktcsdldc.service.base.DanhMucSoBanNganh_TruongCongDanLocalServiceBaseImpl
 * @see qldc.pm.qlktcsdldc.service.DanhMucSoBanNganh_TruongCongDanLocalServiceUtil
 */
public class DanhMucSoBanNganh_TruongCongDanLocalServiceImpl
    extends DanhMucSoBanNganh_TruongCongDanLocalServiceBaseImpl {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never reference this interface directly. Always use {@link qldc.pm.qlktcsdldc.service.DanhMucSoBanNganh_TruongCongDanLocalServiceUtil} to access the danh muc so ban nganh_ truong cong dan local service.
     */
	public void updateDanhMucSoBanNganh_TruongCongDan(long DanhMucSoBanNganhId, String []rowIds, User user)
			throws PortalException, SystemException {

		List<DanhMucSoBanNganh_TruongCongDan> list = findByIDDanhMucSoBanNganh(DanhMucSoBanNganhId);
		if(Validator.isNotNull(list)){
			for(String rowId : rowIds){
				add(DanhMucSoBanNganhId, rowId, user);
			}
		}else{
			int i =0; 
			while(i < list.size() && i< rowIds.length){
				update(list.get(i).getIDDanhMucSoBanNganh_TruongCongDan(), DanhMucSoBanNganhId, rowIds[i], user);
				i++;
			}
			
			if(list.size() < rowIds.length){
				//Adding more 
				while(i< rowIds.length){
					add(DanhMucSoBanNganhId, rowIds[i], user);
					i++;
				}
			}else if(list.size() > rowIds.length){
				//delete 
				while(i< list.size()){
					delete(list.get(i).getIDDanhMucSoBanNganh_TruongCongDan());
					i++;
				}
			}
		}
	}
	private void add(long DanhMucSoBanNganhId, String rowId, User user) throws SystemException, PortalException{
		long IDDanhMucSoBanNganh_TruongCongDan = counterLocalService.increment(DanhMucSoBanNganh_TruongCongDan.class.getName());

		// create a new instance of DanhMucSoBanNganh without saving it to the database
		DanhMucSoBanNganh_TruongCongDan DanhMucSoBanNganh_TruongCongDan = createDanhMucSoBanNganh_TruongCongDan(IDDanhMucSoBanNganh_TruongCongDan);
		
		// set audit fields
		DanhMucSoBanNganh_TruongCongDan.setGroupId(user.getGroupId());
		DanhMucSoBanNganh_TruongCongDan.setCompanyId(user.getCompanyId());
		DanhMucSoBanNganh_TruongCongDan.setUserId(user.getUserId());
		DanhMucSoBanNganh_TruongCongDan.setUserName(user.getScreenName());
		DanhMucSoBanNganh_TruongCongDan.setModifiedDate(new Date());

		// set other fields
		DanhMucSoBanNganh_TruongCongDan.setIDDMDC_TruongCongDan(Long.parseLong(rowId));
		DanhMucSoBanNganh_TruongCongDan.setIDQLKT_DanhMucSoBanNganh(DanhMucSoBanNganhId);
		// update the DanhMucSoBanNganh_TruongCongDan after set all needed parameters in to the
		// database
		 addDanhMucSoBanNganh_TruongCongDan(DanhMucSoBanNganh_TruongCongDan);
	}
	
	private void update(long IDDanhMucSoBanNganh_TruongCongDan, long DanhMucSoBanNganhId, String rowId, User user) throws SystemException, PortalException{
		// create a new instance of DanhMucSoBanNganh without saving it to the database
		DanhMucSoBanNganh_TruongCongDan DanhMucSoBanNganh_TruongCongDan = getDanhMucSoBanNganh_TruongCongDan(IDDanhMucSoBanNganh_TruongCongDan);
		
		// set audit fields
		DanhMucSoBanNganh_TruongCongDan.setGroupId(user.getGroupId());
		DanhMucSoBanNganh_TruongCongDan.setCompanyId(user.getCompanyId());
		DanhMucSoBanNganh_TruongCongDan.setUserId(user.getUserId());
		DanhMucSoBanNganh_TruongCongDan.setUserName(user.getScreenName());
		DanhMucSoBanNganh_TruongCongDan.setModifiedDate(new Date());

		// set other fields
		DanhMucSoBanNganh_TruongCongDan.setIDDMDC_TruongCongDan(Long.parseLong(rowId));
		DanhMucSoBanNganh_TruongCongDan.setIDQLKT_DanhMucSoBanNganh(DanhMucSoBanNganhId);
		// update the DanhMucSoBanNganh_TruongCongDan after set all needed parameters in to the
		// database
		 updateDanhMucSoBanNganh_TruongCongDan(DanhMucSoBanNganh_TruongCongDan);
	}
	private void delete(long IDDanhMucSoBanNganh_TruongCongDan) throws SystemException, PortalException{
		// create a new instance of DanhMucSoBanNganh without saving it to the database
		DanhMucSoBanNganh_TruongCongDan danhMucSoBanNganh_TruongCongDan = getDanhMucSoBanNganh_TruongCongDan(IDDanhMucSoBanNganh_TruongCongDan);
		
		deleteDanhMucSoBanNganh_TruongCongDan(danhMucSoBanNganh_TruongCongDan);
	}
	public List<DanhMucSoBanNganh_TruongCongDan> findByIDDanhMucSoBanNganh(long IDDanhMucSoBanNganh) throws SystemException{
		return danhMucSoBanNganh_TruongCongDanPersistence.findByIDQLKT_DanhMucSoBanNganh(IDDanhMucSoBanNganh);
	}
}
